package com.sdry.service.impl.hpf;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.hpf.BomMapper;
import com.sdry.mapper.hpf.ProductMapper;
import com.sdry.mapper.hpf.ReferenceMapper;
import com.sdry.model.hpf.Bom;
import com.sdry.model.hpf.Product;
import com.sdry.model.hpf.Reference;
import com.sdry.model.llm.Employee;
import com.sdry.model.llm.EmployeeRecord;
import com.sdry.model.llm.Team;
import com.sdry.service.hpf.ProductService;

import net.sf.json.JSONArray;

/**
 * @ClassName: ProductServiceImpl 
 * @Description:产品Service实现
 * @Author hpf
 * @Date 2018年9月20日 下午2:22:11 
 * @version 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {

	@Resource
	private ProductMapper productMapper;
	@Resource
	private BomMapper bomMapper;
	@Resource
	private ReferenceMapper referenceMapper;
	
	/**
	 * 条件查询总数
	 * @param product 条件
	 * @return
	 */
	public Integer countProduct(Product product) {
		
		// TODO Auto-generated method stub
		return productMapper.countProduct(product);
	}

	/**
	 * 分页查询
	 * @param map 条件
	 * @return
	 */
	public List<Product> listPageProduct(Map<String,Object> map) {
		// TODO Auto-generated method stub
		return productMapper.listPageProduct(map);
	}

	/**
	 * 新增产品
	 * @param product
	 * @return
	 */
	public Integer insertProduct(Product product) {
		int isSuccess = -2;
		List<Product> pro = productMapper.getProductByPnum(product.getPnum());
		if(pro.size() == 0){
			int i = productMapper.insertProduct(product);
			if(i > 0){
				isSuccess = 2;
			}
		}
		return isSuccess;
	}

	/**
	 * 修改产品
	 * @param product
	 * @return
	 */
	public Integer updateProduct(Product product) {
		int isSuccess = -3;
		Product p = productMapper.getProductByPid(product.getPid());
		if(p.getPnum().equals(product.getPnum())){
			int i = productMapper.updateProduct(product);
			if(i > 0){
				isSuccess = 3;
			}
		}else{
			List<Product> pro = productMapper.getProductByPnum(product.getPnum());
			if(pro.size() == 0){
				int i = productMapper.updateProduct(product);
				if(i > 0){
					isSuccess = 3;
				}
			}
		}
		return isSuccess;
	}

	/**
	 * 删除产品
	 * @param str
	 * @return
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer deleteProduct(String str) {
		int isSuccess = 0;
		try {
			String[] idArr = str.split(",");
			for (int i = 0; i < idArr.length; i++) {
				isSuccess = productMapper.deleteProduct(Long.parseLong(idArr[i]));
			}
		} catch (Exception e) {
			e.printStackTrace();     
	        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
	        isSuccess = 0;
		}
		return isSuccess;
	}
	
	/**
	 * 校验是否删除产品
	 * @param str
	 * @return
	 */
	
	public Integer deleteProductOfVerify(String str) {
		int isSuccess = -1;
		String[] split = str.split(",");
		for(int i = 0; i < split.length; i ++){
			Long id = Long.parseLong(split[i]);
			List<Reference> referenceList = referenceMapper.getReferenceByProductId(id);
			List<Bom> bomByPid = bomMapper.getBomByPid(id);
			if(referenceList.size() > 0 || bomByPid.size() > 0 ){
				isSuccess = 1;
				break;
			}
		}
		return isSuccess;
	}
	
	/**
	 * 查询所有产品
	 * @param product
	 * @return
	 */
	public List<Product> listAllProduct(Product product) {
		// TODO Auto-generated method stub
		return productMapper.listAllProduct(product);
	}

	/**       
	 * @param productNumber
	 * @return    
	 */
	@Override
	public List<Product> getProductByNumber(String productNumber) {
		return productMapper.getProductByPnum(productNumber);
	}

}
